package com.booth.manage.model;

import java.util.Date;
import java.util.Map;

import com.booth.base.model.Users;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
/***
 * 摊位申请控制器
 * @author Admin
 *
 */
public class BoothProgress extends Model<BoothProgress> {
	
	private static final long serialVersionUID = 1L;
	public static BoothProgress dao=new BoothProgress();
	/***
	 * 根据过滤条件获取数据并分页
	 * @param cardNo 
	 * @param checkEndTime 
	 * @param checkStartTime 
	 * @param applyId 
	 * @param page：当前页
	 * @param pageSize：每页条数
	 * @param name：内容名称
	 * @param beginDate：开始时间
	 * @param endDate：结束时间
	 * @return 返回满足过滤条件的数据结合，否则放回null
	 */
	public Page<BoothProgress> findPage(int page,int pageSize,
			String progressName,String progressStatus,String createStartTime
			, String createEndTime, String checkStartTime,
			String checkEndTime,String businessStartTime,
			String businessEndTime, Integer applyId,Integer flag){
		
		String whereString = " WHERE 1=1 ";
		if(progressName != null && !progressName.equals(""))
		{
			whereString=whereString+" AND t.progress_name like('%"+progressName+"%')";
		}
		if(createStartTime != null && !createStartTime.equals(""))
		{
			whereString=whereString+" AND date_format(t.create_time,'%Y-%m-%d') >= date_format('"+createStartTime+"','%Y-%m-%d')";
		}
		if(createEndTime != null && !createEndTime.equals(""))
		{
			whereString=whereString+" AND date_format(t.create_time,'%Y-%m-%d') <= date_format('"+createEndTime+"','%Y-%m-%d')";
		}
		if(checkStartTime != null && !checkStartTime.equals(""))
		{
			whereString=whereString+" AND date_format(t.check_time,'%Y-%m-%d') >= date_format('"+checkStartTime+"','%Y-%m-%d')";
		}
		if(checkEndTime != null && !checkEndTime.equals(""))
		{
			whereString=whereString+" AND date_format(t.check_time,'%Y-%m-%d') <= date_format('"+checkEndTime+"','%Y-%m-%d')";
		}
		if(businessStartTime != null && !businessStartTime.equals(""))
		{
			whereString=whereString+" AND date_format(t.business_start_time,'%Y-%m-%d') >= date_format('"+businessStartTime+"','%Y-%m-%d')";
		}
		if(businessEndTime != null && !businessEndTime.equals(""))
		{
			whereString=whereString+" AND date_format(t.business_end_time,'%Y-%m-%d') <= date_format('"+businessEndTime+"','%Y-%m-%d')";
		}
		if(progressStatus != null && !progressStatus.equals(""))
		{
			whereString=whereString+" AND t.progress_status = " + progressStatus;
		}
		if(applyId != Integer.MIN_VALUE)
		{
			whereString=whereString+" AND t.user_id = " + applyId;
		}
		if(flag == 1){
			whereString += " and t.progress_status != -1 ";
		}
		if(flag == 2){

			whereString += " and t.progress_status = 1 ";
		}
		return dao.paginate(page, pageSize, "SELECT t.id\n" + 
				",t.progress_name\n" + 
				",t.creator_id\n" + 
				",t.creator_name\n" + 
				",t.progress_desc\n" + 
				",t.create_time\n" + 
				",t.progress_status\n" + 
				",t.progress_statuc_cn\n" + 
				",t.check_desc\n" + 
				",t.updator_id\n" + 
				",t.updator_name\n" + 
				",t.update_time\n" + 
				",t.check_id\n" + 
				",t.check_name\n" + 
				",t.check_time\n" + 
				",str_to_date(date_format(t.business_start_time,'%Y-%m-%d'),'%Y-%m-%d') as business_start_time \n" + 
				",str_to_date(date_format(t.business_end_time,'%Y-%m-%d'),'%Y-%m-%d') as business_end_time\n" + 
				",t.pay_status\n" + 
				",t.booth_id\n" + 
				",t.booth_status\n" + 
				",t.user_id\n" + 
				",a.user_name\n" + 
				"  ", " FROM booth_progress t left join users a on t.user_id = a.user_id "
						+ ""
						+ ""
						+ ""+ whereString +" order by t.create_time desc,t.check_time desc ");
		
	}
	public boolean doProgressStatus(Integer selectId,Integer progress_status,Users curUser) {
		Record record = new Record().set("progress_status", progress_status).set("updator_id", curUser.getUserId()).set("updator_name", curUser.getUserName())
		.set("update_time", new Date()).set("id", selectId);
		
		return Db.update("booth_progress", "id", record);
	}
	public boolean doCheck(Integer selectId, Map<String, Object> map, Users curUser) {
		Date curDate = new Date();
		Record record = new Record()
				.set("progress_status", map.get("progress_status"))
				.set("updator_id", curUser.getUserId())
				.set("updator_name", curUser.getUserName())
				.set("update_time", curDate)
				.set("check_id", curUser.getUserId())
				.set("check_name",curUser.getUserName())
				.set("check_time", curDate)
				.set("check_desc", map.get("check_desc"))
				.set("id", selectId);
				
				return Db.update("booth_progress", "id", record);
	}
	public Page<BoothProgress> findSubletPage(int page, int pageSize, 
			String userName, String boothStatus,
			String businessStartTime, String businessEndTime, Integer applyId,Integer flag) {
		String whereString = " WHERE 1=1 ";
		if(userName != null && !userName.equals(""))
		{
			whereString=whereString+" AND t.user_name like('%"+userName+"%')";
		}
		if(boothStatus != null && !boothStatus.equals(""))
		{
			whereString=whereString+" AND t.booth_status = " +boothStatus;
		}
		if(businessStartTime != null && !businessStartTime.equals(""))
		{
			whereString=whereString+" AND t.business_start_time >= date_format('"+businessStartTime+"','%Y-%m-%d')";
		}
		if(businessEndTime != null && !businessEndTime.equals(""))
		{
			whereString=whereString+" AND t.business_end_time <= date_format('"+businessEndTime+"','%Y-%m-%d')";
		}
		if(applyId != Integer.MIN_VALUE)
		{
			whereString=whereString+" AND t.creator_id =  "+applyId;
		}
		if(flag != null){
			whereString += " AND t.progress_status != -1 ";
		}
		return dao.paginate(page, pageSize, "SELECT t.id\n" + 
				",t.progress_name\n" + 
				",t.creator_id\n" + 
				",t.creator_name\n" + 
				",t.create_time\n" + 
				",str_to_date(date_format(t.business_start_time,'%Y-%m-%d'),'%Y-%m-%d') as business_start_time \n" + 
				",str_to_date(date_format(t.business_end_time,'%Y-%m-%d'),'%Y-%m-%d') as business_end_time\n" + 
				",t.pay_status\n" + 
				",t.booth_id\n" + 
				",t.booth_status\n" + 
				",a.name as booth_name\n" + 
				",a.area\n" + 
				",a.num\n" + 
				",a.address\n" + 
				",b.user_name\n" + 
				",b.phone\n" + 
				"  ", 
				" FROM booth_progress t "
				+ "left join booth a on t.booth_id = a.id \n"
				+ "left join users b on t.user_id = b.user_id\n"
				+ ""
				+ ""
				+ ""+ whereString +" and t.progress_status = 1 order by t.update_time desc");
		
	}
	public boolean doOpenDisable(Integer selectId, Integer boothStatus, Users curUser) {
		Record record = new Record().set("booth_status", boothStatus).set("updator_id", curUser.getUserId()).set("updator_name", curUser.getUserName())
				.set("update_time", new Date()).set("id", selectId);
				
				return Db.update("booth_progress", "id", record);
	}
	public Page<BoothProgress> findMyApply(int page, int pageSize, String progressName, String progressStatus,
			String createStartTime, String createEndTime, String checkStartTime, String checkEndTime,
			String businessStartTime, String businessEndTime, Integer applyId, Integer flag) {
		String whereString = " WHERE 1=1 ";
		if(progressName != null && !progressName.equals(""))
		{
			whereString=whereString+" AND t.progress_name like('%"+progressName+"%')";
		}
		if(createStartTime != null && !createStartTime.equals(""))
		{
			whereString=whereString+" AND date_format(t.create_time,'%Y-%m-%d') >= date_format('"+createStartTime+"','%Y-%m-%d')";
		}
		if(createEndTime != null && !createEndTime.equals(""))
		{
			whereString=whereString+" AND date_format(t.create_time,'%Y-%m-%d') <= date_format('"+createEndTime+"','%Y-%m-%d')";
		}
		if(checkStartTime != null && !checkStartTime.equals(""))
		{
			whereString=whereString+" AND date_format(t.check_time,'%Y-%m-%d') >= date_format('"+checkStartTime+"','%Y-%m-%d')";
		}
		if(checkEndTime != null && !checkEndTime.equals(""))
		{
			whereString=whereString+" AND date_format(t.check_time,'%Y-%m-%d') <= date_format('"+checkEndTime+"','%Y-%m-%d')";
		}
		if(businessStartTime != null && !businessStartTime.equals(""))
		{
			whereString=whereString+" AND date_format(t.business_start_time,'%Y-%m-%d') >= date_format('"+businessStartTime+"','%Y-%m-%d')";
		}
		if(businessEndTime != null && !businessEndTime.equals(""))
		{
			whereString=whereString+" AND date_format(t.business_end_time,'%Y-%m-%d') <= date_format('"+businessEndTime+"','%Y-%m-%d')";
		}
		if(progressStatus != null && !progressStatus.equals(""))
		{
			whereString=whereString+" AND t.progress_status = " + progressStatus;
		}
		if(applyId != Integer.MIN_VALUE)
		{
			whereString=whereString+" AND t.creator_id = " + applyId;
		}
		if(flag == 1){
			whereString += " and t.progress_status != -1 ";
		}
		if(flag == 2){

			whereString += " and t.progress_status = 1 ";
		}
		return dao.paginate(page, pageSize, "SELECT t.id\n" + 
				",t.progress_name\n" + 
				",t.creator_id\n" + 
				",t.creator_name\n" + 
				",t.progress_desc\n" + 
				",t.create_time\n" + 
				",t.progress_status\n" + 
				",t.progress_statuc_cn\n" + 
				",t.check_desc\n" + 
				",t.updator_id\n" + 
				",t.updator_name\n" + 
				",t.update_time\n" + 
				",t.check_id\n" + 
				",t.check_name\n" + 
				",t.check_time\n" + 
				",str_to_date(date_format(t.business_start_time,'%Y-%m-%d'),'%Y-%m-%d') as business_start_time \n" + 
				",str_to_date(date_format(t.business_end_time,'%Y-%m-%d'),'%Y-%m-%d') as business_end_time\n" + 
				",t.pay_status\n" + 
				",t.booth_id\n" + 
				",t.booth_status\n" + 
				",t.user_id\n" + 
				",a.user_name\n" + 
				"  ", " FROM booth_progress t left join users a on t.user_id = a.user_id "
						+ ""
						+ ""
						+ ""+ whereString +" order by t.create_time desc,t.check_time desc ");
	}
	public boolean doPay(Integer selectId, Map<String, Object> params, Users curUser) {
		Date curDate = new Date();
		Record record = new Record()
				.set("pay_status", "1")
				.set("updator_id", curUser.getUserId())
				.set("updator_name", curUser.getUserName())
				.set("update_time", curDate)
				.set("id", selectId);
				return Db.update("booth_progress", "id", record);
	}
}
